/**
 * @file spi2Transmit.c
 * @author  xiaowine (xiaowine@sina.cn)
 * @brief
 * @version 01.00
 * @date    2021-07-24
 *
 * @copyright Copyright (c) {2020}  xiaowine
 *
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2021-07-24 <td>1.0     <td>wangh     <td>内容
 * </table>
 * ******************************************************************
 * *                   .::::
 * *                 .::::::::
 * *                ::::::::::
 * *             ..:::::::::::
 * *          '::::::::::::
 * *            .:::::::::
 * *       '::::::::::::::..        女神助攻,流量冲天
 * *            ..::::::::::::.     永不宕机,代码无bug
 * *          ``:::::::::::::::
 * *           ::::``:::::::::'        .:::
 * *          ::::'   ':::::'       .::::::::
 * *        .::::'      ::::     .:::::::'::::
 * *       .:::'       :::::  .:::::::::' ':::::
 * *      .::'        :::::.:::::::::'      ':::::
 * *     .::'         ::::::::::::::'         ``::::
 * * ...:::           ::::::::::::'              ``::
 * *```` ':.          ':::::::::'                  ::::.
 * *                   '.:::::'                    ':'````.
 * ******************************************************************
 */

/* Private includes ----------------------------------------------------------*/
#include "spiTransmit.h"
#include <rtthread.h>
#include <string.h>
#include "crc16.h"
#include "sensorAPP.h"
#include "stdio.h"
#include "upsAPP.h"
#include "wk2xxx.h"
/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/
#define SPI_PRIORITY   10
#define SPI_STACK_SIZE 1024
#define SPI_TIMESLICE  5
/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
static rt_thread_t tid   = {0};
static rt_mq_t     Spimq = {0};

/* Public variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
struct rx_msg {
    uint8_t msgData[256];
    uint8_t msgSize;
};
/* Private functions ---------------------------------------------------------*/
static void spiRxCallBack(uint8_t port, uint8_t *buff, int len);
/* Private user code ---------------------------------------------------------*/
const SPI_TypeDef *spiPeriph[2] = {SPI1, SPI2};

static void spiTrans_entry(void *parameter)
{
    int spiIndex = (int)parameter;
    wk2xxxInit();

    rt_thread_delay(3000);
    rt_kprintf("spiTrans_entry:%d\r\n", spiIndex);
    while (1) {
        struct rx_msg msg;
        /* receive message */
        if (rt_mq_recv(Spimq, &msg, sizeof(struct rx_msg), RT_WAITING_FOREVER) == RT_EOK) {

            if (((msg.msgSize == 2) && (msg.msgData[0] == 0x68) && (msg.msgData[1] == 0x68)) || (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6) == Bit_RESET)) {
                uint8_t gifr, port, buf[200] = {0}, lenth = 0;
                do {
                    gifr = wkReadSerilData(&port, buf, &lenth);
                    if (lenth) {
                        // rt_kprintf("lenth:%d,port:%d,buf:%02x\r\n", lenth, port, buf[0]);
                        spiRxCallBack(port, buf, lenth);
                    }
                    // rt_kprintf("gifr:%02x,buf:%02x\r\n", gifr, buf[0]);
                } while (gifr > 0);
            } else {
                if (msg.msgData[0] < SPIUSART_NUM) {
                    // rt_kprintf("USART:%d,len:%d,Data[1]:%02x\r\n", msg.msgData[0], msg.msgSize - 1, msg.msgData[1]);
                    wk_TxChars(msg.msgData[0] + 1, msg.msgSize - 1, &msg.msgData[1]);
                }
            }
        }
    }
}

int spiTransmitInit(void)
{
    /* 初始化消息队列 */
    Spimq = rt_mq_create("Spimq", sizeof(struct rx_msg), 5, RT_IPC_FLAG_FIFO);
    /* 创建线程 1，名称是 thread1，入口是 spi2Trans_entry*/
    tid = rt_thread_create("spi2Trans", spiTrans_entry, (void *)SPI2_INDEX, SPI_STACK_SIZE, SPI_PRIORITY, SPI_TIMESLICE);
    /* 如果获得线程控制块，启动这个线程 */
    if (tid != RT_NULL)
        rt_thread_startup(tid);
    return 0;
}
INIT_APP_EXPORT(spiTransmitInit);

static void spiRxCallBack(uint8_t port, uint8_t *buff, int len)
{
    if (port == SPIUSART6)  // U6
    {
        // dtuRXmqPush((uint8_t *)buff, len);
    }
    if (port == SPIUSART7)  // U7
    {
        sensorRXmqPush((uint8_t *)buff, len);
    }
    if (port == SPIUSART8)  // U8
    {
        // rt_kprintf("U8 rec:%d,buff:%s\r\n", len, buff);
        upsRXmqPush(0, (uint8_t *)buff, len);
    }
    if (port == SPIUSART9)  // U9
    {
        // rt_kprintf("U9 rec:%d,buff:%s\r\n", len, buff);
        upsRXmqPush(1, (uint8_t *)buff, len);
    }
}

rt_err_t spiMsgQPush(spiUart_t uartIndex, uint8_t *buff, uint8_t length)
{
    struct rx_msg msg = {0};

    msg.msgData[0] = uartIndex;
    memcpy(&msg.msgData[1], buff, length);
    msg.msgSize = length + 1;
    if (Spimq)
        return rt_mq_send(Spimq, &msg, sizeof(struct rx_msg));
    else
        return RT_EOK;
}

void EXTI9_5_IRQHandler(void)
{
    struct rx_msg msg = {0};
    // rt_kprintf("IN EXTI6_IRQ!!!!\n");
    if (EXTI_GetFlagStatus(EXTI_Line6) != RESET) {
        EXTI_ClearITPendingBit(EXTI_Line6);  //清除LINE1上的中断标志位
        msg.msgData[0] = 0x68;
        msg.msgData[1] = 0x68;
        msg.msgSize    = 2;
        if (Spimq)
            rt_mq_send(Spimq, &msg, sizeof(struct rx_msg));
    }
}
